package com.adie.controller;

import com.adie.pojo.TemplateData;
import com.adie.pojo.WxMssVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

//发送信息
@RestController
public class SendWxMessage {


    //发送消息给家属
    @GetMapping("/pushToFamily")
    public String pushToFamily() {
        return pushToFamily("oauex4jz38sDPXXl-T3s0Un3sUa0");
    }

    //发送信息给志愿者
    @GetMapping("/pushToVolunteers")
    public String pushToVolunteers(){
        return pushToVolunteers("oauex4jz38sDPXXl-T3s0Un3sUa0");
    }


    public String pushToVolunteers(String openid) {
        RestTemplate restTemplate = new RestTemplate();
        //这里简单起见我们每次都获取最新的access_token（时间开发中，应该在access_token快过期时再重新获取）
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户的openid（要发送给那个用户，通常这里应该动态传进来的）
        wxMssVo.setTemplate_id("uoR3mZl7RwsOyx4BRY3g2TOH6HFHETn3FLPm1N5s_8g");//订阅消息模板id
        wxMssVo.setPage("pages/index/index");

        Map<String, TemplateData> m = new HashMap<>(5);
        m.put("time2", new TemplateData("2021-3-14 9:44"));
        m.put("phrase10", new TemplateData("王晓红"));
        m.put("phone_number11",new TemplateData("123456789"));
        m.put("thing4",new TemplateData("据您5公里内有老人走失"));
        m.put("thing8",new TemplateData("疑似在天安门附近和家人走散"));
        wxMssVo.setData(m);
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        return responseEntity.getBody();
    }


    public String pushToFamily(String openid) {
        RestTemplate restTemplate = new RestTemplate();
        //这里简单起见我们每次都获取最新的access_token（时间开发中，应该在access_token快过期时再重新获取）
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户的openid（要发送给那个用户，通常这里应该动态传进来的）
        wxMssVo.setTemplate_id("GVr-7eL1_cwCoZJDrjNjmxX1iXpJ2KFkTrPsw0d3a-Y");//订阅消息模板id
        wxMssVo.setPage("pages/index/index");

        Map<String, TemplateData> m = new HashMap<>(3);
        m.put("thing1", new TemplateData("您家的老人已经走失24小时，请及时报警"));//文字不能太长
        m.put("number3", new TemplateData("001"));
        wxMssVo.setData(m);
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        return responseEntity.getBody();
    }


    //AccessToken相当于进入各种接口的钥匙，拿到这个钥匙才有调用其他各种特殊接口的权限。
    @GetMapping("/getAccessToken")
    public String getAccessToken() {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>();
        params.put("APPID", "wx6823eb6b04587eef");  //
        params.put("APPSECRET", "e2387f22a548a798e6946ac812edff63");  //
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
        String body = responseEntity.getBody();
        JSONObject object = JSON.parseObject(body);
        String Access_Token = object.getString("access_token");
        String expires_in = object.getString("expires_in");
        System.out.println("有效时长expires_in：" + expires_in);
        return Access_Token;
    }


}
